<?php

class AdminAction extends CommonAction{
public function index()
{
$condition['is_delete'] = 0;
$this->assign('default_map',$condition);
parent::index();
}
public function trash()
{
$condition['is_delete'] = 1;
$this->assign('default_map',$condition);
parent::index();
}
public function add()
{
$this->assign('role_list',M('Role')->where('is_delete = 0')->findAll());
$this->display();
}
public function edit() {
$id = intval($_REQUEST ['id']);
$condition['is_delete'] = 0;
$condition['id'] = $id;
$vo = M(MODULE_NAME)->where($condition)->find();
$this->assign ( 'vo',$vo );
$this->assign('role_list',M('Role')->where('is_delete = 0')->findAll());
$this->display ();
}
public function set_effect()
{
$id = intval($_REQUEST['id']);
$ajax = intval($_REQUEST['ajax']);
$info = M(MODULE_NAME)->where('id='.$id)->getField('adm_name');
$c_is_effect = M(MODULE_NAME)->where('id='.$id)->getField('is_effect');
if(conf('DEFAULT_ADMIN')==$info)
{
$this->ajaxReturn($c_is_effect,l('DEFAULT_ADMIN_CANNOT_EFFECT'),1)	;
}
$n_is_effect = $c_is_effect == 0 ?1 : 0;
M(MODULE_NAME)->where('id='.$id)->setField('is_effect',$n_is_effect);
save_log($info.l('SET_EFFECT_'.$n_is_effect),1);
$this->ajaxReturn($n_is_effect,l('SET_EFFECT_'.$n_is_effect),1)	;
}
public function insert() {
B('FilterString');
$data = M(MODULE_NAME)->create ();
$this->assign('jumpUrl',u(MODULE_NAME.'/add'));
if(!check_empty($data['adm_name']))
{
$this->error(L('ADM_NAME_EMPTY_TIP'));
}
if(!check_empty($data['adm_password']))
{
$this->error(L('ADM_PASSWORD_EMPTY_TIP'));
}
if($data['role_id']==0)
{
$this->error(L('ROLE_EMPTY_TIP'));
}
if(M('Admin')->where("adm_name='".$data['adm_name']."'")->count()>0)
{
$this->error(L('ADMIN_EXIST_TIP'));
}
$log_info = $data['adm_name'];
$data['adm_password'] = md5(trim($data['adm_password']));
$list=M(MODULE_NAME)->add($data);
if (false !== $list) {
save_log($log_info.L('INSERT_SUCCESS'),1);
$this->success(L('INSERT_SUCCESS'));
}else {
save_log($log_info.L('INSERT_FAILED'),0);
$this->error(L('INSERT_FAILED'));
}
}
public function update() {
B('FilterString');
$data = M(MODULE_NAME)->create ();
$log_info = M(MODULE_NAME)->where('id='.intval($data['id']))->getField('adm_name');
$this->assign('jumpUrl',u(MODULE_NAME.'/edit',array('id'=>$data['id'])));
if(!check_empty($data['adm_password']))
{
unset($data['adm_password']);
}
else
{
$data['adm_password'] = md5(trim($data['adm_password']));
}
if($data['role_id']==0)
{
$this->error(L('ROLE_EMPTY_TIP'));
}
if(conf('DEFAULT_ADMIN')==$log_info)
{
$adm_session = es_session::get(md5(conf('AUTH_KEY')));
$adm_name = $adm_session['adm_name'];
if($log_info!=$adm_name)
$this->error(l('DEFAULT_ADMIN_CANNOT_MODIFY'));
if($data['is_effect']==0)
{
$this->error(l('DEFAULT_ADMIN_CANNOT_EFFECT'));
}
}
$list=M(MODULE_NAME)->save ($data);
if (false !== $list) {
save_log($log_info.L('UPDATE_SUCCESS'),1);
$this->success(L('UPDATE_SUCCESS'));
}else {
save_log($log_info.L('UPDATE_FAILED'),0);
$this->error(L('UPDATE_FAILED'),0,$log_info.L('UPDATE_FAILED'));
}
}
public function delete() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['adm_name'];
if(conf('DEFAULT_ADMIN')==$data['adm_name'])
{
$this->error ($data['adm_name'].l('DEFAULT_ADMIN_CANNOT_DELETE'),$ajax);
}
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->setField ( 'is_delete',1 );
if ($list!==false) {
save_log($info.l('DELETE_SUCCESS'),1);
$this->success (l('DELETE_SUCCESS'),$ajax);
}else {
save_log($info.l('DELETE_FAILED'),0);
$this->error (l('DELETE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
public function restore() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['adm_name'];
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->setField ( 'is_delete',0 );
if ($list!==false) {
save_log($info.l('RESTORE_SUCCESS'),1);
$this->success (l('RESTORE_SUCCESS'),$ajax);
}else {
save_log($info.l('RESTORE_FAILED'),0);
$this->error (l('RESTORE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
public function foreverdelete() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['adm_name'];
if(conf('DEFAULT_ADMIN')==$data['adm_name'])
{
$this->error ($data['adm_name'].l('DEFAULT_ADMIN_CANNOT_DELETE'),$ajax);
}
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->delete();
if ($list!==false) {
save_log($info.l('FOREVER_DELETE_SUCCESS'),1);
$this->success (l('FOREVER_DELETE_SUCCESS'),$ajax);
}else {
save_log($info.l('FOREVER_DELETE_FAILED'),0);
$this->error (l('FOREVER_DELETE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
public function set_default()
{
$adm_id = intval($_REQUEST['id']);
$admin = M('Admin')->getById($adm_id);
if($admin)
{
M('Conf')->where("name = 'DEFAULT_ADMIN'")->setField('value',$admin['adm_name']);
$sys_configs = M('Conf')->findAll();
$config_str = "<?php\n";
$config_str .= "return array(\n";
foreach($sys_configs as $k=>$v)
{
$config_str.="'".$v['name']."'=>'".addslashes($v['value'])."',\n";
}
$config_str.=");\n ?>";
$filename = get_real_path().'public/sys_config.php';
if (!$handle = fopen($filename,'w')) {
$this->error(l('OPEN_FILE_ERROR').$filename);
}
if (fwrite($handle,$config_str) === FALSE) {
$this->error(l('WRITE_FILE_ERROR').$filename);
}
fclose($handle);
save_log(l('CHANGE_DEFAULT_ADMIN'),1);
clear_cache();
$this->success(L('SET_DEFAULT_SUCCESS'));
}
else
{
$this->error(L('NO_ADMIN'));
}
}
}

?>